//
//  XDTabBar.swift
//  weibo
//
//  Created by lucy on 2016/10/22.
//  Copyright © 2016年 com.huashengHuaSheng. All rights reserved.
//

import UIKit

class XDTabBar: UITabBar {

    //定义闭包
    var closure : (()->())?
    
    //MARK-首写方法走这个初始化
    override init(frame :CGRect){
        super.init(frame: frame)
        setupUI()

    }
    
    private func setupUI(){
        
        addSubview(composedButton)
    
    }
    
    //MARK-xib
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK-按钮点击事件
    @objc private func buttonClick(){
    
        closure?()
    
    }
    
    //MARK-添加中间那个按钮
    lazy var composedButton : UIButton = {
    
        let button = UIButton(setImageName: "tabbar_compose_icon_add", setBackGroundName: "tabbar_compose_button", target: self, action: #selector(buttonClick))
        return button
    
    
    }()
    
    
    //MARK-为中间按钮进行布局
    override func layoutSubviews() {
        super.layoutSubviews()
        let W = SCREENWIDTH * 0.2
        
        var index : CGFloat = 0
        for value in subviews{
        
            if value .isKind(of: NSClassFromString("UITabBarButton")!){
                value.frame.size.width = W
                value.frame.origin.x = index * W
            
                index = index+1
                if index==2 {
                    index = index+1
                }
            }
        
        }
        
        composedButton.center.x = SCREENWIDTH*0.5
        composedButton.center.y = frame.size.height * 0.5
    }

}
















